package com.example.day09d_1;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.annotations.NonNull;


public class Recy_Fragment extends Fragment implements V_Main {


    private RecyclerView mRecy;
    private ArrayList<GetBean.ResultsBean> getBeans;
    private Recy_Adapter adapter;
    boolean muserVisibleHint;
    private P_Main p_main;
    int a = 1;
    private SmartRefreshLayout mSm;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_alank, container, false);
        p_main = new P_Main(this);
        p_main.getData();
        initView(view);
        return view;
    }

    private void initView(@NonNull final View itemView) {
        mSm = (SmartRefreshLayout) itemView.findViewById(R.id.sm);
        mRecy = (RecyclerView) itemView.findViewById(R.id.recy);
        mRecy.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecy.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        getBeans = new ArrayList<>();
        adapter = new Recy_Adapter(getBeans, getActivity());
        mRecy.setAdapter(adapter);
        mSm.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                a=1;
                p_main.getData();

            }
        });
        mSm.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                 a++;
                p_main.getData();
            }
        });


    }

    @Override
    public void showData(List<GetBean.ResultsBean> getdata) {
        getBeans.addAll(getdata);
        adapter.notifyDataSetChanged();
        mSm.finishRefresh();
        mSm.finishLoadMore();

    }

    @Override
    public void showToast(String str) {
        Toast.makeText(getActivity(), "str", Toast.LENGTH_SHORT).show();
    }


}
